<?php

class Principal extends CI_Controller {

//    function __construct() {
//        // Call the Model constructor
//        parent::__construct();
//        if ($sessionData = $this->session->userdata('datosUser')) {
//            $this->lang->load($sessionData['idioma'], $sessionData['idioma']);
//            $this->clear_cache();
//        } else {
//            //redirect('frontend/login/login');
//        }
//    }

    function clear_cache() {
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
    }

    public function index() {
        if ($this->session->userdata('datosUser')) {
            $sessionData = $this->session->userdata('datosUser');
            $this->lang->load($sessionData['idioma'], $sessionData['idioma']);
            $this->cargamenu->carga('backend/principal/principal');
        } else {
            redirect('frontend/login/login');
        }
    }

    public function cambioIdioma() {
        $sessionData = $this->session->userdata('datosUser');
        $idioma = $this->input->get('lang');
        $this->load->model('backend/principal/mprincipal');
        $this->mprincipal->cambiarIdiomaUser($sessionData, $idioma);
        $sessionData['idioma'] = $idioma;
        $this->session->set_userdata('datosUser', $sessionData);
        $this->load->library('user_agent');
        $url = substr($this->agent->referrer(), stripos($this->agent->referrer(), 'backend') - 1);
        redirect($url);
    }

    public function cambioIdiomaFrontend() {
        $idioma = $this->input->get('lang');
        $this->lang->load($idioma, $idioma);
        $this->load->helper(array('form', 'url'));
        $this->load->library('user_agent');
//       echo $this->agent->referrer();
        if (!strpos($this->agent->referrer(), 'sobresantander') && !strpos($this->agent->referrer(), 'sobreSantander')) {
            redirect('frontend/home');
        } else {
            redirect('/frontend/home/sobreSantander');
        }
    }

    public function salir() {
        $this->session->sess_destroy();
        redirect('');
    }

    public function paginaPreferida() {
        $this->load->library('user_agent');
        $url = substr($this->agent->referrer(), stripos($this->agent->referrer(), 'backend') - 1);
        $this->load->model('backend/principal/mprincipal');
        $sessionData = $this->session->userdata('datosUser');
        $this->mprincipal->paginaPrincipal($sessionData, $url);
        redirect($url);
    }

    public function configuracionPaginaPreferida() {
        $pagina = $this->input->post('pagina');
        if ($pagina != "0") {
            $this->load->model('backend/principal/mprincipal');
            $sessionData = $this->session->userdata('datosUser');
            $this->mprincipal->paginaPrincipal($sessionData, $pagina);
        }
        redirect('backend/personal/configuracion');
    }

}